import { ValueT } from '@/type';
import { WatchViewI } from '../type';
import CanvasUtils from '@/utils/CanvasUtils';

/** 绘制图标参数 */
export interface ImageStyle {
	/** x位置 */
	x: ValueT<number>;
	/** y位置 */
	y: ValueT<number>;
	/** y位置 */
	width: ValueT<number>;
	/** y位置 */
	height: ValueT<number>;
	/** 图标 */
	icon: ValueT<CanvasImageSource>;
	/** 圆角半径 */
	radius?: ValueT<number | null>;
	/** 透明度 */
	transparency?: ValueT<number>;
	/** 是否显示 */
	show?: ValueT<boolean>;
}

/** 图标绘制类 */
export default class ImageView implements WatchViewI {

	public Z_Index: ValueT<number>;

	/** 画板类 */
	public canvas: CanvasUtils;

	/** 绘制文本参数 */
	private save: ImageStyle;

	/**
	 * 构造函数
	 * @param canvas 画板类
	 * @param save 绘制图标参数
	 * @param zIndex 绘制级别(最大最上)
	 */
	constructor(canvas: CanvasUtils, save: ImageStyle, zIndex?: ValueT<number>) {
		this.canvas = canvas;
		this.save = save;
		this.Z_Index = zIndex || { Value: 0 };
	}

	/** @override */
	public Draw() {
		const { save, canvas } = this;
		const { x, y, width, height, icon, radius, transparency, show } = save;
		if (show && !show.Value) { return; }
		canvas.Save();
		if (transparency) {
			const transparencyV = transparency.Value;
			canvas.GlobalAlpha = transparencyV;
		}
		if (radius && radius.Value !== null) {
			canvas.RoundImage({
				Spot: { X: x.Value, Y: y.Value },
				Width: width.Value,
				Height: height.Value
			},
				icon.Value,
				radius.Value
			);
		} else {
			canvas.DrawImage({
				Rect: {
					Spot: { X: x.Value, Y: y.Value },
					Width: width.Value,
					Height: height.Value
				},
				Image: icon.Value,
			});
		}
		canvas.Restore();
	}
}
